package cn.fboost.mtos.service.impl;

import cn.fboost.mtos.dto.Response;
import cn.fboost.mtos.dto.internal.Session;
import cn.fboost.mtos.dto.request.AddressAddRequest;
import cn.fboost.mtos.dto.request.AddressUpdateRequest;
import cn.fboost.mtos.entity.UserAddress;
import cn.fboost.mtos.mapper.UserAddressMapper;
import cn.fboost.mtos.service.AddressService;
import cn.fboost.mtos.util.ContextHolderUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.Objects;

@Service
public class AddressServiceImpl implements AddressService {
    private final UserAddressMapper addressMapper;

    public AddressServiceImpl(UserAddressMapper addressMapper) {
        this.addressMapper = addressMapper;
    }

    @Override
    public Response AddressList() {
        Session session = ContextHolderUtil.getCurrentSession();
        return Response.builder().success().msg("获取成功").data(addressMapper.listByUserId(session.getId())).build();
    }

    @Override
    @Transactional
    public Response AddressAdd(AddressAddRequest request) {
        Session session = ContextHolderUtil.getCurrentSession();

        UserAddress address = new UserAddress();
        address.setUserId(session.getId());
        address.setName(request.getName());
        address.setTelephone(request.getTelephone());
        address.setAddress(request.getAddress());
        address.setIsDefault(request.getIsDefault());

        // 如果是添加的默认地址，先清除其他地址选项
        if (address.getIsDefault()) {
            addressMapper.cleanDefault(session.getId());
        }

        if (addressMapper.add(address) == 0) {
            // 如果添加失败，手动回滚事务
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Response.builder().failure().msg("添加失败，请重试").build();
        }

        return Response.builder().success().msg("添加成功").data(address).build();
    }

    @Override
    @Transactional
    public Response AddressUpdate(AddressUpdateRequest request) {
        Session session = ContextHolderUtil.getCurrentSession();

        UserAddress address = addressMapper.getById(request.getId());
        if (address == null || !Objects.equals(address.getUserId(), session.getId())) {
            // 如果地址不存在或者不属于当前用户，返回错误
            return Response.builder().failure().msg("地址不存在").build();
        }

        address.setName(request.getName());
        address.setTelephone(request.getTelephone());
        address.setAddress(request.getAddress());
        address.setIsDefault(request.getIsDefault());

        // 如果是添加的默认地址，先清除其他地址选项
        if (address.getIsDefault()) {
            addressMapper.cleanDefault(session.getId());
        }

        if (addressMapper.update(address) == 0) {
            // 如果更新失败，手动回滚事务
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Response.builder().failure().msg("更新失败，请重试").build();
        }

        return Response.builder().success().msg("修改成功").data(address).build();
    }

    @Override
    public Response AddressDelete(Integer id) {
        Session session = ContextHolderUtil.getCurrentSession();
        if (addressMapper.deleteInUserId(session.getId(), id) == 0) {
            return Response.builder().failure().msg("删除失败，请重试").build();
        }

        return Response.builder().success().msg("删除成功").build();
    }
}
